/**
 * @author Haidar Osman
 */
var ShapeArray = function(parentShape, name, memberArray) {
	this.members = [];
	this.memberArray = memberArray;
	var that = this;
	this.group = new Kinetic.Group({
		name : name
	});
	this.parentGroup = parentShape.group;
	this.parentShape = parentShape;
	if(name == "attributes") {
		this.isMethod = false;
	} else {
		this.isMethod = true;
	}
	this.addMember = function(header) {
		if(header !== "") {
			this.memberArray.addMember(header);
			var newShape = new BasicShape(header, that.isMethod, that, that.memberArray.getMember(header));
		} else {
			var newShape = new BasicShape(header, that.isMethod, that, new Member(header));
		}

		this.members[this.members.length] = newShape;
		this.parentGroup.add(newShape.group);
		// this.parentShape.addMember

	}
	this.update = function() {
		var i = 0;
		for( i = 0; i < that.members.length; i++) {
			this.parentGroup.remove(that.members[i].group);
		}
		var height = 26;
		if( typeof this.members[0] !== "undefined") {
			height = this.members[0].rect.height;
			// alert(height);
		}
		this.members = [];
		this.addMember("");
		this.members[0].rect.height = height;
		this.members[0].signature.fontSize = (this.members[0].rect.height - 2 * this.members[0].signature.padding) / 2;
		this.members[0].rect.width = this.members[0].signature.width + 4 * this.members[0].signature.padding;
		for( i = 0; i < that.memberArray.members.length; i++) {
			var newShape = new BasicShape(that.memberArray.members[i].name, that.isMethod, that, that.memberArray.members[i]);
			newShape.rect.height = height;
			newShape.signature.fontSize = (newShape.rect.height - 2 * newShape.signature.padding) / 2;
			newShape.rect.width = newShape.signature.width + 4 * newShape.signature.padding;
			this.members[this.members.length] = newShape;
			this.parentGroup.add(newShape.group);

		}
		if(that.isVisible) {
			that.show();
		} else {
			that.hide();
		}		
	}
	this.removeMember = function(header) {
		var i = 0;
		for( i = 0; i < that.members.length; i++) {
			if(that.members[i].signature.text == header) {
				that.parentGroup.remove(that.members[i].group);
				that.members.splice(i, 1);
				break;
			}
		}
		this.memberArray.removeMember(header);

	}

	this.isVisible = true;
	this.hide = function() {
		that.isVisible = false;
		var i = 0;
		for( i = 0; i < that.members.length; i++) {
			that.members[i].rect.visible = false
			that.members[i].signature.visible = false
			that.members[i].group.visible = false;
			//that.parentGroup.remove(that.members[i].group);
			//that.parentGroup.remove(that.members[i].signature);
		}
	}
	this.show = function() {
		that.isVisible = true;
		var i = 0;
		for( i = 0; i < that.members.length; i++) {
			that.members[i].rect.visible = true
			that.members[i].signature.visible = true
			that.members[i].group.visible = true;
			//that.parentGroup.add(that.members[i].group);
			// that.parentGroup.add(that.members[i].signature);
		}
	}
	this.getMember = function(name) {
		var i = 0;
		for( i = 0; i < that.members.length; i++) {
			// alert("looping "+that.members[i].group.name);
			if(that.members[i].group.name == name) {
				return that.members[i];
			}
		}
		return null;
	}
}